from pynput.keyboard import Key, Controller  # 导入按键控制
import time  # 导入时间包，调用sleep函数让琴键有时间间隔
import threading  # 导入线程包
from pywinauto import Desktop


# 演奏函数
def play_piano(music, keytime):
    for n in music:
        if n.isupper():
            keyboard.press(Key.shift)
            time.sleep(0.001)
            keyboard.press(n.lower())
            time.sleep(keytime - 0.001)
            keyboard.release(n.lower())
            keyboard.release(Key.shift)
        elif n == "|" or n == ")":
            pass
        elif n in "!@$%^*(":
            keyboard.press(Key.shift)
            time.sleep(0.001)
            keyboard.press("1245689"["!@$%^*(".index(n)])
            time.sleep(keytime - 0.001)
            keyboard.release("1245689"["!@$%^*(".index(n)])
            keyboard.release(Key.shift)
        elif n != " " and n != "-":
            keyboard.press(n)
            if music.index(n) != len(music) - 1 and music[music.index(n) + 1] == ")":
                time.sleep(keytime / 2)
            else:
                time.sleep(keytime)
            keyboard.release(n)
        elif n == "-":
            time.sleep(2 * keytime)
        else:
            time.sleep(keytime)


def thread_play(play_piano, keytime, right="", left="", rightThumb="", rightIndexFinger="", leftThumb="",
                leftIndexFinger=""):
    # 运行线程
    # 运行线程
    rt = threading.Thread(target=play_piano, args=(right, keytime))  # 主旋律线程
    lt = threading.Thread(target=play_piano, args=(left, keytime))  # 和弦线程
    rtt = threading.Thread(target=play_piano, args=(rightThumb, keytime))  # 右手拇指线程
    rift = threading.Thread(target=play_piano, args=(rightIndexFinger, keytime))  # 右手食指线程
    ltt = threading.Thread(target=play_piano, args=(leftThumb, keytime))  # 左手拇指线程
    lift = threading.Thread(target=play_piano, args=(leftIndexFinger, keytime))  # 右手食指线程
    rt.start()
    lt.start()
    rtt.start()
    rift.start()
    ltt.start()
    lift.start()
    rt.join()
    lt.join()
    rtt.join()
    rift.join()
    ltt.join()
    lift.join()


if __name__ == '__main__':
    # 准备演奏
    # 控制键盘
    keyboard = Controller()
    # 切换到vue键盘钢琴(auto piano)网页
    keyboard.press(Key.cmd)
    time.sleep(1)
    keyboard.press("d")
    keyboard.release("d")
    keyboard.release(Key.cmd)
    # 链接的方式点击桌面任务栏的正在运行程序print_control_identifiers()
    dlg = Desktop(backend="uia").任务栏.运行中的程序.child_window(title="Google Chrome - 1 个运行窗口", auto_id="Chrome",
                                                         control_type="Button").click()
    time.sleep(2)
    # 右手
    right = " dfl|klh-dfl|klh-dfl|klh-fdh|f - dfl|" \
            "klh-dfl|klh --|"
    left = "--|4 8 q -|5 9 w -|6 0 e -|6 - 6 -|" \
           "4 8 q -|5 9 w -|"
    thread_play(play_piano, 0.35, right, left)
    right = "lz"
    thread_play(play_piano, 0.05, right)
    right = "x ---|-- dfl|klh-dfl|klh-dfl|" \
            "klh-fdh|f - dfl|klh-dfl|klh-lkz|" \
            "l -t i |"
    left = "6 0 e r |t -6 -|4 8 q -|5 9 w -|" \
           "6 0 e -|6 - 6 -|4 8 q -|5 9 w -|" \
           "8 w --|"
    thread_play(play_piano, 0.35, right, left)
    right = "u - opf|"
    left = "8 ---|"
    rightThumb = "w ---|"
    thread_play(play_piano, 0.35, right, left, rightThumb)
    right = "dfs-opf|dfs-opf|dfs-dsa|" \
            "s - opf|dfs-opf|dfs-hsd|f -- i|u - opf|" \
            "dfs-opf|dfs-opf|dfs-dsa|s - opf|" \
            "dfs-opf|dfs-hsd|s -- i|u"
    left = "4 8 q -|5 9 w -|6 0 e -|" \
           "6 -e -|4 8 q -|5 9 w -|8 w t -|8 ---|" \
           "4 8 q -|5 9 w -|6 0 e -|6 -6 -|" \
           "4 8 q -|5 9 w -|8 w t -|8"
    thread_play(play_piano, 0.35, right, left)
